"""
sphere.py --                                                                                            
                                                                                                                                                                                                        
It sets a sphere's geometric properties.                                              
                                                                                                                                                                                                            
Date of creation: 2007-03-17                                                                        
                                                                                                                                                                                                
Copyright  Robotics and Automation Group, Pontificia Universidad Javeriana - Cali.                  
    Freddy Naranjo Perez, fnaranjo@puj.edu.co                                                                   
    Antonio Alejandro Matta Gomez amatta@puj.edu.co                                      
    Julian David Colorado, jdcolorado@puj.edu.co                           
    Juan Camilo Acosta Mejia, jcacosta@puj.edu.co                                    
                                                                                                                                                                                                        
See the file "license.terms" for information on usage and redistribution of this file, and for a    
DISCLAIMER OF ALL WARRANTIES.
"""

import vtk
from vtk.util.colors import *

class Sphere:
  def __init__( self, ren, geomdim, posorient, lighting, color ):
        """ Inits a sphere's 3d object and its associated color and lighting"""
        
        sphere = vtk.vtkSphere()
        sphere.SetCenter(0, 0, 0)
        sphere.SetRadius(geomdim)
        
        # The sample function generates a distance function from the implicit
        # function (which in this case is the sphereHole). This is then contoured to
        # get a polygonal surface.
        theSphereSample = vtk.vtkSampleFunction()
        theSphereSample.SetImplicitFunction(sphere)
        theSphereSample.SetModelBounds(-0.08, 0.08, -0.08, 0.08, -0.08, 0.08)
        theSphereSample.SetSampleDimensions(60, 60, 60)
        theSphereSample.ComputeNormalsOn()
        
        #vtk.vtkContourFilter applied in order to get a polygonal surface.
        theSphereSurface = vtk.vtkContourFilter()
        theSphereSurface.SetInputConnection(theSphereSample.GetOutputPort())
        theSphereSurface.SetValue(0, 0.0)

        #Decimation algorithm applied in order to optimaze polygonal surface.
        deci = vtk.vtkDecimatePro()
        deci.SetInputConnection(theSphereSurface.GetOutputPort())
        deci.SetTargetReduction(0.1)
        deci.PreserveTopologyOn()

        #Smoothing applied in order to get a better surface.
        smooth= vtk.vtkSmoothPolyDataFilter()
        smooth.SetInputConnection(deci.GetOutputPort())
        smooth.SetNumberOfIterations(20)

        #Normal's computation.
        normals = vtk.vtkPolyDataNormals()
        normals.SetInputConnection(smooth.GetOutputPort())
        normals.FlipNormalsOn()

        #Normal's passed to the poly data mapper.
        SphereMapper = vtk.vtkPolyDataMapper()
        SphereMapper.SetInputConnection(normals.GetOutputPort())
        SphereMapper.ScalarVisibilityOff()

        self.sphereActor = vtk.vtkActor()
        self.sphereActor.SetMapper(SphereMapper)
        self.sphereActor.GetProperty().SetInterpolationToGouraud()
        self.sphereActor.GetProperty().SetColor(color)
        self.sphereActor.GetProperty().SetSpecular(.3)
        self.sphereActor.GetProperty().SetSpecularPower(30)
        self.sphereActor.SetPosition( posorient[0], posorient[1], posorient[2] )
        self.sphereActor.SetOrientation( posorient[3], posorient[4], posorient[5] )
       
            
        
    
        
    
    

        
        
            
        
    

    
  
  